package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.RemainingTimes;
import com.itheima.domain.mongo.Sound;
import com.itheima.domain.vo.SoundVo;
import com.itheima.service.mongo.SoundService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.LinkedList;
import java.util.List;

@Service
public class SoundServiceImpl implements SoundService {

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    IdService idService;
    @Override//上传传音
    public void save(Sound sound) {
        //给音频设置一个sid,给大数据团队使用
        Long sid = idService.getNextId(ConstantUtil.SOUND_ID);
        sound.setSid(sid);

        mongoTemplate.save(sound);
    }

    @Override//重置次数
    public void resetRemainingTimes(List<Long> userIds) {

        for (Long userId : userIds) {
            Query query = new Query(
                Criteria.where("userId").is(userId)
            );
            Update update = new Update();
            update.set("remainingTimes",10);
            mongoTemplate.updateFirst(query,update, RemainingTimes.class);
        }
    }

    @Override//新建次数
    public void saveRemainingTimes(long userId) {
        RemainingTimes remainingTimes = new RemainingTimes();
        remainingTimes.setUserId(userId);
        remainingTimes.setRemainingTimes((byte)10);
        mongoTemplate.save(remainingTimes);
    }

    @Override////查询传音vo
    public SoundVo findTH(long sid) {
        Query query = new Query(
                Criteria.where("sid").is(sid)
        );
        Sound sound = mongoTemplate.findOne(query, Sound.class);
        //封装进vo
        SoundVo soundVo = new SoundVo();
        soundVo.setId(sound.getUserId().intValue());//设置用户id
        //头像和昵称在消费者中提供
        soundVo.setGender(sound.getGender());//设置性别
        soundVo.setAge(sound.getAge());//设置年龄
        soundVo.setSoundUrl(sound.getSoundUrl());//设置声音地址
        //剩余次数在消费者中封装
        return soundVo;
    }

    @Override//查询非自己的声音库
    public List<Long> findSids(Long userId) {
        Query query = new Query(
          Criteria.where("userId").ne(userId)
        );
        List<Sound> sounds = mongoTemplate.find(query, Sound.class);
        List<Long> sids = new LinkedList<>();

        if (CollUtil.isNotEmpty(sounds)) {
            for (Sound sound : sounds) {
                sids.add(sound.getSid());
            }
        }


        return sids;
    }

    @Override//查询剩余次数
    public byte findRemTimes(Long userId) {
        byte remainingTimes = 0;
        Query query = new Query(
          Criteria.where("userId").is(userId)
        );
        RemainingTimes one = mongoTemplate.findOne(query, RemainingTimes.class);
        if (one != null){
             remainingTimes = one.getRemainingTimes();
        }
        return remainingTimes;
    }

    @Override//使用次数
    public void useTimes(Long userId, byte remTimes) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        Update update = new Update();
        update.set("remainingTimes",remTimes);
        mongoTemplate.updateFirst(query,update, RemainingTimes.class);
    }

    //声音垃圾回收
    public void soundGC(List<String> urls) {
        //删除
        for (String url : urls) {
            Query query = new Query(
                    Criteria.where("soundUrl").lt(url)
            );
            mongoTemplate.remove(query, Sound.class);
        }

        //sid重排
        //重新查询
        List<Sound> sounds = mongoTemplate.find(null, Sound.class);
        //重排
        if (CollUtil.isNotEmpty(sounds)) {
            long i = 0;
            i++;
            Long sid = idService.getNextId(ConstantUtil.SOUND_ID);
            Update update = new Update();
            update.set("sid",i);
            //update.set("sid",sid);
            mongoTemplate.updateFirst(null,update, Sound.class);
        }
    }

    @Override//查找过期声音
    public List<String> findOldSoundUrl(long halfYearAgo) {
        Query query = new Query(
                Criteria.where("created").lt(halfYearAgo)
        );
        List<Sound> sounds = mongoTemplate.find(query, Sound.class);
        if (CollUtil.isNotEmpty(sounds)) {
            List<String> urls = new LinkedList<>();
            for (Sound sound : sounds) {
                urls.add(sound.getSoundUrl());
            }
            soundGC(urls);

            return urls;
        }

        return null;
    }
}
